package com.sp2p.action.front;

import java.io.IOException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import net.sf.json.JSONObject;

import com.shove.Convert;
import com.shove.data.DataException;
import com.shove.util.JSONUtils;
import com.shove.util.SqlInfusion;
import com.shove.web.util.ExcelUtils;
import com.sp2p.constants.IConstants;
import com.sp2p.entity.Admin;
import com.sp2p.entity.User;
import com.sp2p.service.FrontMyPaymentService;
import com.sp2p.service.MyHomeService;
import com.sp2p.service.OperationLogService;
import com.sp2p.service.UserService;
import com.sp2p.service.admin.FundManagementService;
import com.thoughtworks.xstream.io.json.JsonWriter.Format;

public class FrontMyPaymentAction extends BaseFrontAction {

	private FrontMyPaymentService frontpayService;
	private UserService userService;
	 private MyHomeService myHomeService; 
	 private FundManagementService fundManagementService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}



	public FrontMyPaymentService getFrontpayService() {
		return frontpayService;
	}

	public void setFrontpayService(FrontMyPaymentService frontpayService) {
		this.frontpayService = frontpayService;
	}
	
	public void setMyHomeService(MyHomeService myHomeService) {
		this.myHomeService = myHomeService;
	}
	
	public void setFundManagementService(FundManagementService fundManagementService) {
		this.fundManagementService = fundManagementService;
	}

	/**
	 * 成功的借款 
	 * 
	 * @return
	 * @throws SQLException
	 * @throws DataException
	 */
	public String queryMySuccessBorrowList() throws Exception {
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		pageBean.setPageNum(SqlInfusion.FilteSqlInfusion(request("curPage")));
		String startTime = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("startTime"), null));
		String endTime = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("endTime"), null));
		String title = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("title"), null));
		
		int borrowStatus = request("borrowStatus") == null ? -1 : Convert
				.strToInt(request("borrowStatus"), -1);
		endTime = changeEndTime(endTime);

		

		if (borrowStatus == -1) {
			frontpayService.queryMySuccessBorrowList(pageBean, userId,
					startTime, endTime, title, -1);
		} else {// 还款中的借款 已还完的借款
			frontpayService.queryMySuccessBorrowList(pageBean, userId,
					startTime, endTime, title, borrowStatus);
		}

		List<Map<String, Object>> lists = pageBean.getPage();

		if (lists != null) {
			for (Map<String, Object> map : lists) {
				if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_NET_VALUE)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_NET_VALUE_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_SECONDS)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_SECONDS_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_GENERAL)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_GENERAL_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_FIELD_VISIT)) {
					map.put("borrowWay",IConstants.BORROW_TYPE_FIELD_VISIT_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE)) {
					map.put("borrowWay",IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE_STR);
				}else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_INSTITUTION_FLOW)) {
					map.put("borrowWay",IConstants.BORROW_TYPE_INSTITUTION_FLOW_STR);
				}

				if (map.get("borrowStatus").toString().equals(
						IConstants.BORROW_STATUS_4 + "")) {
					map.put("borrowStatus", IConstants.BORROW_STATUS_4_STR);
				} else if (map.get("borrowStatus").toString().equals(
						IConstants.BORROW_STATUS_5 + "")) {
					map.put("borrowStatus", IConstants.BORROW_STATUS_5_STR);
				}
			}
		}
		this.setRequestToParamMap();
		return SUCCESS;
	}
	
	/**
	 * 导出成功借款,正在还款的借款，已还借款 的数据excel
	 * @return
	 */
	public String exportSuccessBorrow(){
		
		Long userId = this.getUserId();// 获得用户编号
		Integer status=Convert.strToInt(request("status"), -1);
		pageBean.pageNum = 1;
		pageBean.setPageSize(5000);
		try {               
			String startTime=SqlInfusion.FilteSqlInfusion(Convert.strToStr(request().getParameter("startTime"),null));
			String endTime=SqlInfusion.FilteSqlInfusion(Convert.strToStr(request().getParameter("endTime"),null));
			endTime = changeEndTime(endTime);
			String title =SqlInfusion.FilteSqlInfusion(Convert.strToStr(request().getParameter("title"),null));
			//中文乱码转换
			if(StringUtils.isNotBlank(title)){
				title = URLDecoder.decode(title,"UTF-8");
			}
			//成功借款
			frontpayService.queryMySuccessBorrowList(pageBean, userId,
					startTime, endTime, title, status);
			if(pageBean.getPage()==null)
			{
				getOut().print("<script>alert(' 导出记录条数不能为空! ');window.history.go(-1);</script>");
				return  null;
			}
			if(pageBean.getPage().size()>IConstants.EXCEL_MAX)
			{
			getOut().print("<script>alert(' 导出记录条数不能大于50000条 ');window.history.go(-1);</script>");
			return  null;
			}
			List<Map<String, Object>> list = pageBean.getPage();
			if (list == null) {
				list = new ArrayList<Map<String, Object>>();
			}
			if (list != null) {
				for (Map<String, Object> map : list) {
					if (map.get("borrowWay").toString().equals(
							IConstants.BORROW_TYPE_NET_VALUE)) {
						map.put("borrowWay", IConstants.BORROW_TYPE_NET_VALUE_STR);
					} else if (map.get("borrowWay").toString().equals(
							IConstants.BORROW_TYPE_SECONDS)) {
						map.put("borrowWay", IConstants.BORROW_TYPE_SECONDS_STR);
					} else if (map.get("borrowWay").toString().equals(
							IConstants.BORROW_TYPE_GENERAL)) {
						map.put("borrowWay", IConstants.BORROW_TYPE_GENERAL_STR);
					} else if (map.get("borrowWay").toString().equals(
							IConstants.BORROW_TYPE_FIELD_VISIT)) {
						map
								.put("borrowWay",
										IConstants.BORROW_TYPE_FIELD_VISIT_STR);
					} else if (map.get("borrowWay").toString().equals(
							IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE)) {
						map.put("borrowWay",
								IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE_STR);
					}

					if (map.get("borrowStatus").toString().equals(
							IConstants.BORROW_STATUS_4 + "")) {
						map.put("borrowStatus", IConstants.BORROW_STATUS_4_STR);
					} else if (map.get("borrowStatus").toString().equals(
							IConstants.BORROW_STATUS_5 + "")) {
						map.put("borrowStatus", IConstants.BORROW_STATUS_5_STR);
					}
				}
			}
			HSSFWorkbook wb=null;
			if(status==-1){
				wb = ExcelUtils.exportExcel("成功借款", pageBean.getPage(),
						new String[] { "标题", "借款类型", "借款金额(￥)", "年利率(%)", "还款期限(月)", "借款时间",
						          "偿还本息(￥)","已还本息(￥)","未还本息(￥)",
								"状态" }, new String[] { "borrowTitle", "borrowWay",
								"borrowAmount", "annualRate", "deadline",
								"publishTime", "stillTotalSum","hasPI","hasSum","borrowStatus" });
			}else if(status==4){
				wb = ExcelUtils.exportExcel("正在还款的借款", pageBean.getPage(),
						new String[] { "标题", "借款类型", "借款金额(￥)", "年利率(%)", "还款期限(月)", "借款时间",
						          "偿还本息(￥)","已还本息(￥)","未还本息(￥)"
								 }, new String[] { "borrowTitle", "borrowWay",
								"borrowAmount", "annualRate", "deadline",
								"publishTime", "stillTotalSum","hasPI","hasSum" });
			}else if(status==5){
				
				wb = ExcelUtils.exportExcel("已还完的借款", pageBean.getPage(),
						new String[] { "标题", "借款类型", "借款金额(￥)", "年利率(%)", "还款期限(月)", "借款时间",
						          "偿还本息(￥)","已还本息(￥)","已还逾期罚息(￥)"
								 }, new String[] { "borrowTitle", "borrowWay",
								"borrowAmount", "annualRate", "deadline",
								"publishTime", "stillTotalSum","stillTotalSum","hasFI" });
			}
			
			this.export(wb, new Date().getTime() + ".xls");
			
		} catch (SQLException e) {
		
			e.printStackTrace();
		} catch (DataException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 正在还款的借款
	 * @return
	 * @throws SQLException
	 * @throws DataException
	 */
	@SuppressWarnings("deprecation")
	public String queryMyPayingBorrowList() throws Exception {
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		pageBean.setPageNum(request("curPage"));
		String startTime =SqlInfusion.FilteSqlInfusion( Convert.strToStr(request("startTime"), null));
		String endTime = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("endTime"), null));
		String title = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("title"), null));
		/*int borrowStatus = paramMap.get("borrowStatus") == null ? -1 : Convert
				.strToInt(paramMap.get("borrowStatus"), -1);*/
		
		int borrowStatus = IConstants.BORROW_STATUS_4;
		endTime = changeEndTime(endTime);

		if (borrowStatus == -1) {
			frontpayService.queryMySuccessBorrowList(pageBean, userId,
					startTime, endTime, title, -1);
		} else {// 还款中的借款 已还完的借款
			frontpayService.queryMySuccessBorrowList(pageBean, userId,
					startTime, endTime, title, borrowStatus);
		}

		List<Map<String, Object>> lists = pageBean.getPage();
		int syPeriod=0;
		Double hkjd=0d;
		String repayPeriod="";
		String hasDeadline="";
		String repayDate="";
		int recivedFI =0;
		if (lists != null) {
			for (Map<String, Object> map : lists) {
				if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_NET_VALUE)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_NET_VALUE_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_SECONDS)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_SECONDS_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_GENERAL)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_GENERAL_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_FIELD_VISIT)) {
					map.put("borrowWay",IConstants.BORROW_TYPE_FIELD_VISIT_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE)) {
					map.put("borrowWay",IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE_STR);
				}else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_INSTITUTION_FLOW)) {
					map.put("borrowWay",IConstants.BORROW_TYPE_INSTITUTION_FLOW_STR);
				}
				if(null!=map.get("repayPeriod")&&map.get("repayPeriod").toString().length()>0&&null!=map.get("hasDeadline")&&null!=map.get("repayDate")&&map.get("hasDeadline").toString().length()>0){
					repayPeriod=map.get("repayPeriod")+"";
					hasDeadline=map.get("hasDeadline")+"";
					repayDate=map.get("repayDate")+"";
					syPeriod=Integer.parseInt(repayPeriod.split("/")[1])-Integer.parseInt(hasDeadline);
					map.put("syPeriod",syPeriod);
					map.put("recivedFI",recivedFI);
					hkjd=Double.parseDouble(hasDeadline)/Double.parseDouble(repayPeriod.split("/")[1])*100;
					map.put("hkjd",String.format("%.2f",hkjd));
					if(map.get("isDayThe").toString().equals("1")){	//不是天标
						String theMonth = repayDate.substring(5, 7);
						String theYear = repayDate.substring(0,4);
						int month = (Integer.parseInt(theMonth) + Integer.parseInt(hasDeadline));
						if(month/12>0){
							theYear = Integer.parseInt(theYear) + month/12+"";
							theMonth = month -12*(month/12)+"";
						}else{
							theMonth = month+"";
						}
						if(theMonth.length()==1){
							theMonth = "0"+theMonth;
						}
						repayDate = theYear+"-"+theMonth+repayDate.substring(7,10);
						map.put("repayDate", repayDate);
						
						
					}
				}

				
				
			}
		}
		request().setAttribute("userId", user.getId());
		this.setRequestToParamMap();
		return SUCCESS;
	}
	
	/**
	 * 已还完借款
	 * @return
	 * @throws SQLException
	 * @throws DataException
	 */
	public String queryMyPayoffBorrowList() throws Exception {
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		pageBean.setPageNum(request("curPage"));
		String startTime =SqlInfusion.FilteSqlInfusion( request("startTime"));
		String endTime =SqlInfusion.FilteSqlInfusion( request("endTime"));
		String title =SqlInfusion.FilteSqlInfusion( request("title"));
		int borrowStatus = IConstants.BORROW_STATUS_5;
		endTime = changeEndTime(endTime);

		frontpayService.queryMySuccessBorrowList(pageBean, userId,
					startTime, endTime, title, borrowStatus);
		
		List<Map<String, Object>> lists = pageBean.getPage();
		Double hkjd=0d;
		String repayPeriod="";
		String hasDeadline="";
		if (lists != null) {
			for (Map<String, Object> map : lists) {
				if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_NET_VALUE)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_NET_VALUE_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_SECONDS)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_SECONDS_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_GENERAL)) {
					map.put("borrowWay", IConstants.BORROW_TYPE_GENERAL_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_FIELD_VISIT)) {
					map
							.put("borrowWay",
									IConstants.BORROW_TYPE_FIELD_VISIT_STR);
				} else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE)) {
					map.put("borrowWay",
							IConstants.BORROW_TYPE_INSTITUTION_GUARANTEE_STR);
				}else if (map.get("borrowWay").toString().equals(
						IConstants.BORROW_TYPE_INSTITUTION_FLOW)) {
					map.put("borrowWay",
							IConstants.BORROW_TYPE_INSTITUTION_FLOW_STR);
				}
				if(null!=map.get("repayPeriod")&&map.get("repayPeriod").toString().length()>0&&
						null!=map.get("hasDeadline")&&map.get("hasDeadline").toString().length()>0){
					repayPeriod=map.get("repayPeriod")+"";
					hasDeadline=map.get("hasDeadline")+"";
					hkjd=Double.parseDouble(hasDeadline)/Double.parseDouble(repayPeriod.split("/")[1])*100;
					map.put("hkjd",String.format("%.2f",hkjd));
				}
				map.put("borrowStatus", IConstants.BORROW_STATUS_5_STR);
				
			}
		}
		this.setRequestToParamMap();
		return SUCCESS;
	}

	/**
	 * 正在还款的借款详情
	 * 
	 * @return
	 * @throws SQLException
	 * @throws DataException
	 */
	public String queryPayingDetails() throws Exception {
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		Long borrowId = paramMap.get("borrowId") == null ? -1 : Convert.strToLong(paramMap.get("borrowId"), -1);// 
		
		int status = -1;
		if(SqlInfusion.FilteSqlInfusion(paramMap.get("status")) != null){
			status = Convert.strToInt(paramMap.get("status"), -1);
		}
		// 获得统计信息
		Map<String, String> map = null;

		pageBean.setPageSize(1000);
		
		if(user != null){
			Map<String, String> accmountStatisMap = myHomeService
				.queryAccountStatisInfo(user.getId());
			request().setAttribute("accmountStatisMap", accmountStatisMap);
			request().setAttribute("user", user);
		}
		
		if(borrowId == -1){
			return null;
		}
		frontpayService.queryPayingDetails(pageBean, borrowId,status);
		map = frontpayService.queryOneBorrowInfo(userId,
				borrowId);
		
		List<Map<String, Object>> lists = pageBean.getPage();
		if (lists != null) {
			for (Map<String, Object> mp : lists) {
				if (Convert.strToInt(mp.get("repayStatus").toString(), -1) == IConstants.PAYING_STATUS_NON) {
					mp.put("repayStatus", IConstants.PAYING_STATUS_NON_STR);
				} else if (Convert.strToInt(mp.get("repayStatus").toString(),
						-1) == IConstants.PAYING_STATUS_PAYING) {
					mp.put("repayStatus", IConstants.PAYING_STATUS_PAYING_STR);
				}else if(Convert.strToInt(mp.get("repayStatus").toString(),
						-1) == IConstants.PAYING_STATUS_SUCCESS){//已偿还完
					mp.put("repayStatus", IConstants.PAYING_STATUS_SUCCESS_STR);
				}
				if(null!=mp.get("repayPeriod")&&mp.get("repayPeriod").toString().length()>0){
					mp.put("repayPeriod", mp.get("repayPeriod").toString().split("/")[0]);
				}
			}
		}

		// map 首次加载时，为Null
		if(map != null){
			request().setAttribute("borrowTitle", map.get("borrowTitle"));
			request().setAttribute("borrowAmount", map.get("borrowAmount"));
			request().setAttribute("annualRate", map.get("annualRate"));
			request().setAttribute("deadline", map.get("deadline"));
			request().setAttribute("isDayThe", map.get("isDayThe"));
			request().setAttribute("hasDeadline", map.get("hasDeadline"));
			if (Convert.strToInt(map.get("paymentMode").toString(), -1) == IConstants.PAY_WAY_MONTH) {
				request().setAttribute("paymentMode", IConstants.PAY_WAY_MONTH_STR);
			} else {
				request().setAttribute("paymentMode", IConstants.PAY_WAY_RATE_STR);
			}
			request().setAttribute("publishTime", map.get("publishTime"));
		}
		return SUCCESS;
	}

	/**
	 * 还款明细账
	 * 
	 * @return
	 * @throws DataException
	 * @throws SQLException
	 */
	public String queryAllDetails() throws Exception {
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		pageBean.setPageNum(request("curPage"));
		String startTime = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("startTime"), null));
		String endTime = SqlInfusion.FilteSqlInfusion(Convert.strToStr(request("endTime"), null));
		String title =SqlInfusion.FilteSqlInfusion( Convert.strToStr(request("title"), null));
		endTime = changeEndTime(endTime);

		pageBean.setPageSize(IConstants.PAGE_SIZE_10);

		frontpayService.queryAllDetails(pageBean, userId, startTime, endTime,
				title);
		this.setRequestToParamMap();
		return SUCCESS;
	}
	
	/**
	 * 还款明细账，的数据导出excel文件
	 * @return
	 */
	public String exportrepayment(){
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		
		pageBean.pageNum = 1;
		pageBean.setPageSize(5000);
		try {
			//还款明细账
			String startTime=SqlInfusion.FilteSqlInfusion(Convert.strToStr(request().getParameter("startTime"),null));
			String endTime=SqlInfusion.FilteSqlInfusion(Convert.strToStr(request().getParameter("endTime"),null));
			endTime = changeEndTime(endTime);
			String title =SqlInfusion.FilteSqlInfusion(Convert.strToStr(request().getParameter("title"),null));
			//中文乱码转换
			if(StringUtils.isNotBlank(title)){
				title = URLDecoder.decode(title,"UTF-8");
			}
			frontpayService.queryAllDetails(pageBean, userId, startTime, endTime,
					title);
			if(pageBean.getPage()==null)
			{
				getOut().print("<script>alert(' 导出记录条数不能为空! ');window.history.go(-1);</script>");
				return  null;
			}
			if(pageBean.getPage().size()>IConstants.EXCEL_MAX)
			{
			getOut().print("<script>alert(' 导出记录条数不能大于50000条 ');window.history.go(-1);</script>");
			return  null;
			}
			List<Map<String, Object>> list = pageBean.getPage();
			if (list == null) {
				list = new ArrayList<Map<String, Object>>();
			}
			if (list != null) {
				for (Map<String, Object> map : list) {		

					if (map.get("repayStatus").toString().equals(
							1+ "")) {
						map.put("repayStatus","未偿还");
					} else  {
						map.put("repayStatus","已偿还");
					}
				}
			}
			
			
			HSSFWorkbook wb= ExcelUtils.exportExcel("还款明细账", pageBean.getPage(),
						new String[] { "标题", "第几期", "应还款日期", "实际还款日期", "本期应还本息(￥)", "利息(￥)",
						          "逾期罚款(￥)","逾期天数(天)","还款状态"
								 }, new String[] { "borrowTitle", "repayPeriod",
								"repayDate", "realRepayDate", "forPI",
								"stillInterest", "lateFI","lateDay","repayStatus" });
			
			
			this.export(wb, new Date().getTime() + ".xls");
			
		} catch (Exception e) {
		
			e.printStackTrace();
		} 
		return null;
	}
	

	/**
	 * 借款明细账
	 * 
	 * @return
	 * @throws SQLException
	 * @throws DataException
	 */
	public String queryBorrowInvestorInfo() throws Exception {
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		Long userId = user.getId();// 获得用户编号
		pageBean.setPageNum(request("curPage"));
		String investor =SqlInfusion.FilteSqlInfusion( request("investor"));

		frontpayService.queryBorrowInvestorInfo(pageBean, userId, investor);
		this.setRequestToParamMap();
		return SUCCESS;
	}
	
	/**
	 * 还款数据显示
	 * @return
	 * @throws DataException
	 * @throws SQLException
	 */
	public String queryMyPayData() throws Exception{
		long payId = request("payId") == null?-1:Convert.strToLong(request("payId"), -1);
		Map<String,String> payMap = frontpayService.queryMyPayData(payId);
		request().setAttribute("payMap", payMap);
		return SUCCESS;
	}
	
	/**
	 * 提交还款记录
	 * @return
	 * @throws Exception 
	 */
	public String submitPay() throws Exception{
		User user = (User)session().getAttribute("user");
		JSONObject obj = new JSONObject();
		String code = (String) session().getAttribute("invest_checkCode");
		String _code = SqlInfusion.FilteSqlInfusion(paramMap.get("code") == null ? "" : paramMap
				.get("code"));
		String id = SqlInfusion.FilteSqlInfusion(paramMap.get("id") == null ? "" : paramMap
				.get("id"));
		long idLong = Convert.strToLong(id, -1L);
		String pwd = SqlInfusion.FilteSqlInfusion(paramMap.get("pwd") == null ? "" : paramMap
				.get("pwd"));
		/*if (StringUtils.isBlank(pwd.trim())) {
			obj.put("msg", "密码不能为空");
			JSONUtils.printObject(obj);
			return null;
		}
		if (!code.equals(_code)) {
			obj.put("msg", "验证码错误");
			JSONUtils.printObject(obj);
			return null;
		}*/
		Map<String, String> map =frontpayService.submitPay(idLong,user.getId(),pwd,getBasePath(),user.getUserName());
		String result =Convert.strToStr( map.get("ret_desc"),"");
		obj.put("msg", result);
		JSONUtils.printObject(obj);
		return null;
	}
	
	/**
	 * 批量还款
	 * @return
	 * @throws Exception 
	 */
	public String batchSubmitPay(){
		System.out.println("batchSubmitPay.........batchSubmitPay............");
		String repaymentId = SqlInfusion.FilteSqlInfusion(paramMap.get("repaymentId"));
		String stillAmount = SqlInfusion.FilteSqlInfusion(paramMap.get("stillAmount"));
		String borrowId = SqlInfusion.FilteSqlInfusion(paramMap.get("borrowId"));
		String hasDeadline =SqlInfusion.FilteSqlInfusion( paramMap.get("hasDeadline"));
		try {
			User user = (User)session().getAttribute("user");
			JSONObject obj = new JSONObject();
//			String id = SqlInfusion.FilteSqlInfusion(paramMap.get("ids"));
			
			String[] ids = borrowId.split(",");
	        for (int i = 0; i < ids.length; i++)
	        {
	            long idLong = Convert.strToLong(ids[i], -1L);
	    		Map<String, String> map;
				
					map = frontpayService.submitPay(idLong,user.getId(),user.getPassword(),getBasePath(),user.getUserName());
					String result =SqlInfusion.FilteSqlInfusion(Convert.strToStr( map.get("ret_desc"),""));
		    		obj.put("msg", result);
					JSONUtils.printObject(obj);
	        }
        } catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public String updateReleased()
	    throws SQLException, IOException
	{
	    String id = SqlInfusion.FilteSqlInfusion(paramMap.get("id"));
	    long result = 0;
	    try
	    {
	        if (result > 0)
	        {
	            
	            JSONUtils.printStr("1");
	        }
	        else
	        {
	            JSONUtils.printStr("2");
	        }
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	        JSONUtils.printStr("2");
	    }
	    return null;
	}

	public static String changeEndTime(String endTime) {
		if (endTime != null && !endTime.equals("")) {
			String[] strs = endTime.split("-");
			// 结束日期往后移一天,否则某天0点以后的数据都不呈现
			Date date = new Date();// 取时间
			long time = Date.UTC(Convert.strToInt(strs[0], -1) - 1900, Convert
					.strToInt(strs[1], -1) - 1, Convert.strToInt(strs[2], -1),
					0, 0, 0);
			date.setTime(time);
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
			date = calendar.getTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			return formatter.format(date);
		}
		return null;
	}
	
	public static Date changeStrToDate(String dateTime){
		if(dateTime != null){
			String[] strs = dateTime.split("-");
			int ind = strs[2].indexOf(" ");
			if(ind >= 0){
				strs[2] = strs[2].substring(0,ind+1);
			}
			Date date = new Date();// 取时间
			long time = date.UTC(Convert.strToInt(strs[0], -1) - 1900, Convert
					.strToInt(strs[1], -1) - 1, Convert.strToInt(strs[2].trim(), -1),
					0, 0, 0);
			date.setTime(time);
			return date;
		}
		return null;
	}
	
	/**
	 * 前台用户还款
	 * 
	 * @return
	 * @throws DataException
	 * @throws SQLException
	 */
	public String userRepayment() throws Exception {
		String repaymentId = SqlInfusion.FilteSqlInfusion(paramMap.get("repaymentId"));
		String stillAmount = SqlInfusion.FilteSqlInfusion(paramMap.get("stillAmount"));
		String borrowId = SqlInfusion.FilteSqlInfusion(paramMap.get("borrowId"));
		String hasDeadline =SqlInfusion.FilteSqlInfusion( paramMap.get("hasDeadline"));
		User user = (User) session().getAttribute(IConstants.SESSION_USER);
		// 自动还款管理
		if (user == null) {
			return "noLogin";
		}
		if (repaymentId == null || repaymentId.length() <= 0)
			return SUCCESS;
		try {
			fundManagementService.updateRepayment(repaymentId, stillAmount, borrowId, hasDeadline, user.getId(), user.getLastIP());
			/*String[] ids = borrowId.split(",");
	        for (int i = 0; i < ids.length; i++)
	        {
	            long idLong = Convert.strToLong(ids[i], -1L);
	    		Map<String, String> map;
					map = frontpayService.submitPay(idLong,user.getId(),user.getPassword(),getBasePath(),user.getUserName());
					String result =SqlInfusion.FilteSqlInfusion(Convert.strToStr( map.get("ret_desc"),""));
	        }*/
			operationLogService.addOperationLog("t_recharge_withdraw_info", user.getUserName(), IConstants.INSERT, user.getLastIP(), 0, " 用户还款",
					2);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return null;
	}
	
	public void setOperationLogService(OperationLogService operationLogService) {
		this.operationLogService = operationLogService;
	}
}
